package cn.stacey.spring.ssm.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{
	Logger logger=LoggerFactory.getLogger(LoginInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
	//从session里面取用户信息
		String user= (String) request.getSession().getAttribute("user");
	//如果有，说明登录了，返回true
		if(user!=null&&user.length()>0) {
			logger.debug("用户已经登录");
			return true;}else {
				//没有的话返回false,返回登录界面
				request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request, response);
				logger.debug("用户未登录");
				return false;
			}
		}

		
	

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	
		
	}

}
